#include <string>
#include <algorithm>
#include <boost/foreach.hpp>
#include "Entity.h"
#include "../data/MimeDefaults.h"

#define foreach BOOST_FOREACH

using namespace kc_sf;

// --------------------------------------------------------------- //

Entity::Entity()
: eContentType( ContentType( "text", "plain" ) ),
  eEncoding( _7Bit ),
  eCharset( ASCII ),
  e_parent( NULL )
{
}

// --------------------------------------------------------------- //

Entity::Entity ( const Entity &entity )
: eContentType( entity.eContentType )
{
	e_parent = entity.e_parent;
	eData = entity.eData;
	eBoundary = entity.eBoundary;
	eCharset = entity.eCharset;
	eContentType = entity.eContentType;
	eEncoding = entity.eEncoding;

	foreach( Header *header, entity.e_headers )
	{
		e_headers.push_back( header->clone() );
	};

	foreach( Entity *child, entity.e_children )
	{
		e_children.push_back( new Entity( *child ) );
	};
}

// --------------------------------------------------------------- //

Entity & Entity::operator = ( const Entity &entity )
{
	e_parent = entity.e_parent;
	eData = entity.eData;
	eBoundary = entity.eBoundary;
	eCharset = entity.eCharset;
	eContentType = entity.eContentType;
	eEncoding = entity.eEncoding;

	foreach( Header *header, entity.e_headers )
	{
		e_headers.push_back( header->clone() );
	};

	foreach( Entity *child, entity.e_children )
	{
		e_children.push_back( new Entity( *child ) );
	};

	return *this;
}

// --------------------------------------------------------------- //

Entity::~Entity()
{
	foreach( Header *header, e_headers )
	{
		if( header != NULL )
			delete header;
	}

	foreach( Entity *entity, e_children )
	{
		if( entity != NULL )
			delete entity;
	}
}

// --------------------------------------------------------------- //

void Entity::addHeader( Header *h )
{
	e_headers.push_back( h );
}

// --------------------------------------------------------------- //

void Entity::addChild( Entity *entity )
{
	entity->e_parent = this;
	e_children.push_back( entity );
}

// --------------------------------------------------------------- //

void Entity::setParent( Entity* parent )
{
    e_parent = parent;
}

// --------------------------------------------------------------- //

std::vector <Header *> Entity::getHeaders()
{
	return std::vector<Header *>( e_headers.begin(), e_headers.end() );
}

// --------------------------------------------------------------- //

bool Entity::isSinglepart() const
{
	return e_children.empty();
}

// --------------------------------------------------------------- //

bool Entity::isMultipart() const
{
	return !e_children.empty();
}
